<?php

class DesComponent
{
    private $key = '12345678';

    public function __construct($key = '')
    {
        if ($this->key) {
            $this->key = $key;
            //$this->key = pack("H10", $this->key);
        }
    }

    /**
     *
     * @param string $string
     * @param int $do_base64
     * @param int $do_gzip
     * @return string
     */
    public function encrypt($string, $do_base64 = 1, $do_gzip = 1, $do_hex = 0)
    {
        $ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF);
        $iv=null;
        foreach ($ivArray as $element) {
            $iv.=CHR($element);
        }

        $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC );
        $string = $this->pkcs5Pad ( $string, $size );

        $data =  mcrypt_encrypt(MCRYPT_DES, $this->key, $string, MCRYPT_MODE_CBC, $iv);

        if ($do_base64) {
            //base64
            $data = base64_encode($data);
        }

        if ($do_gzip) {
            //gzip
            $data = gzencode($data);
        }

        if ($do_hex) {
            $data = $this->strToHex($data);
        }

        return $data;
    }

    /**
     *
     * @param unknown $string
     * @param unknown $do_base64
     * @param unknown $do_gzip
     * @return string
     */
    public function decrypt($string, $do_base64 = 1, $do_gzip = 1, $do_hex = 0)
    {
        $ivArray=array(0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF);

        //an_dump($ivArray);
        if ($do_hex) {
            //$ivArray = array(97,98,99,100,101,102,103,104);
            $ivArray = array(85,70,83,68,85,70,83,68);
            //$ivArray = $this->stringToByteArray($this->key, 'utf-8');
        }
        //an_dump($ivArray);

        $iv=null;
        foreach ($ivArray as $element) {
            $iv.=CHR($element);
        }

        //var_dump($iv);exit;
        if ($do_hex) {
            //
            $string = $this->hexToStr($string);
        }
        //var_dump($string);

        if ($do_gzip) {
            //gzip
            $string = $this->_gzdecode($string);
        }

        if ($do_base64) {
            //base64
            $string = base64_decode($string);
        }

        //echo("****");
        //echo($string);
        //echo("****");
        $result =  mcrypt_decrypt(MCRYPT_DES, $this->key, $string, MCRYPT_MODE_CBC, $iv);
        $result = $this->pkcs5Unpad( $result );

        return $result;
    }


    public function pkcs5Pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen ( $text ) % $blocksize);
        return $text . str_repeat ( chr ( $pad ), $pad );
    }

    public function pkcs5Unpad($text)
    {
        $pad = ord ( $text {strlen ( $text ) - 1} );
        if ($pad > strlen ( $text )) {
            return $text.'12121';
            //return false;
        }
        if (strspn ( $text, chr ( $pad ), strlen ( $text ) - $pad ) != $pad) {
            return $text.'13333';
            //return false;
        }

        return substr ( $text, 0, - 1 * $pad );
    }

    public function _gzdecode($data)
    {
        return gzinflate(substr($data,10,-8));
    }

    private function _hex2bin($str){
        $len = strlen($str)/2;
        $re = '';
        for($i=0;$i<$len;$i++){
            $pos = $i*2;
            $re .= chr(hexdec(substr($str,$pos,1))<<4) | chr(hexdec(substr($str,$pos+1,1)));
        }
        return $re;
    }

    //汉字转换为16进制编码
    private function strToHex($s) {
        $r = "";
        $hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
        for ($i=0; $i<strlen($s); $i++) {$r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);}
        return $r;
    }

    private function hexToStr($s)
    {
        $r = "";
        for ( $i = 0; $i<strlen($s); $i += 2)
        {
            $x1 = ord($s{$i});
            $x1 = ($x1>=48 && $x1<58) ? $x1-48 : $x1-97+10;
            $x2 = ord($s{$i+1});
            $x2 = ($x2>=48 && $x2<58) ? $x2-48 : $x2-97+10;
            $r .= chr((($x1 << 4) & 0xf0) | ($x2 & 0x0f));
        }
        return $r;
    }

    private function strToHex1($string)
    {
        $hex="";
        for   ($i=0; $i<strlen($string); $i++)
        $hex.=dechex(ord($string[$i]));
        $hex=strtoupper($hex);
        return   $hex;
    }

    private function  hexToStr1($hex)
    {
        $string="";
        for   ($i=0;$i<strlen($hex)-1;$i+=2)
        $string.=chr(hexdec($hex[$i].$hex[$i+1]));
        return   $string;
    }

    private function stringToByteArray($str,$charset)
    {
        $str = iconv($charset,'UTF-16',$str);
        preg_match_all('/(.)/s',$str,$bytes);  //注：本文的盗版已经有了。不过，提示一下读者，这里的正则改了。
        $bytes=array_map('ord',$bytes[1]) ;
        return $bytes;
    }

    private function byteArrayToString($bytes,$charset) {

        $bytes=array_map('chr',$bytes);
        $str=implode('',$bytes);
        $str = iconv('UTF-16',$charset,$str);
        return $str;

    }
}
?>